package com.alpaca.recommend.hbase.activerecord;

import com.alpaca.recommend.bean.*;

import java.util.HashMap;
import java.util.Map;

public class TableMapping {

    private final Map<Class<? extends HModel<?>>, String> modelToTableMap = new HashMap<Class<? extends HModel<?>>, String>();

    private static TableMapping me = new TableMapping();

    private TableMapping() {
        putTable(URecommend.class, "u_recommends");
        putTable(UItem.class, "u_items");
        putTable(Item.class, "items");
        putTable(TagItem.class, "tag_items");
        putTable(UTags.class, "u_tags");
        putTable(User.class, "users");
        putTable(TagUser.class, "tag_users");
    }

    public static TableMapping me() {
        return me;
    }

    public void putTable(Class<? extends HModel<?>> modelClass, String table) {
        modelToTableMap.put(modelClass, table);
    }

    @SuppressWarnings("rawtypes")
    public String getTable(Class<? extends HModel> modelClass) {
        String table = modelToTableMap.get(modelClass);
        if (table == null)
            throw new RuntimeException("The Table mapping of model: " + modelClass.getName() + " not exists. Please add mapping to ActiveRecordPlugin: activeRecordPlugin.addMapping(tableName, YourModel.class).");

        return table;
    }
}
